package com.xzhuang.task.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

public class HttpUtil {

	public static String POST(String url) throws Exception {
		String result = "";
		try {
			byte[] dataByte = null;
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(new URI(url));
			httpClient.getParams().setIntParameter("http.socket.timeout", 15000);
			httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
			httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);
			HttpResponse httpResponse = httpClient.execute(httpPost);
			httpClient.clearResponseInterceptors();
			// 获取返回的数据
			HttpEntity httpEntity = httpResponse.getEntity();
			if (httpEntity != null) {
				result = EntityUtils.toString(httpEntity , "UTF-8").trim();
				httpPost.abort();
			}
			httpPost.releaseConnection();
			httpClient.getConnectionManager().shutdown();

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
		return result;
	}

	public static String POSTENTITY(String url, Map<String, Object> argsMap) throws Exception {
		String result = null;
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(new URI(url));
		httpClient.getParams().setIntParameter("http.socket.timeout", 5000);
		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
		// 设置参数
		// UrlEncodedFormEntity encodedFormEntity = new
		// UrlEncodedFormEntity(setHttpParams(argsMap), "UTF-8");
		// httpPost.setEntity(encodedFormEntity);
		// 执行请求
		HttpResponse httpResponse = httpClient.execute(httpPost);
		httpClient.clearResponseInterceptors();
		// 获取返回的数据
		HttpEntity httpEntity = httpResponse.getEntity();
		if (httpEntity != null) {
			result = EntityUtils.toString(httpEntity , "UTF-8").trim();
			httpPost.abort();
		}
		httpPost.releaseConnection();
		httpClient.getConnectionManager().shutdown();


		return result;

	}

	@SuppressWarnings("finally")
	public static String GET(String url) throws Exception {
		BufferedReader in = null;
		byte[] dataByte = null;
		String content = null;
		try {
			// 定义HttpClient
			DefaultHttpClient client = new DefaultHttpClient();
			// 实例化HTTP方法
			HttpGet request = new HttpGet();
			request.setURI(new URI(url));
			HttpResponse response = client.execute(request);

			HttpEntity httpEntity = response.getEntity();
			if (httpEntity != null) {
				content = EntityUtils.toString(httpEntity , "UTF-8").trim();
				request.abort();
				
			}
			request.releaseConnection();
			
			client.getConnectionManager().shutdown();
			 
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
		return content;
	}

	private static byte[] getData(HttpEntity httpEntity) throws Exception {

		BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);

		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

		bufferedHttpEntity.writeTo(byteArrayOutputStream);

		byte[] responseBytes = byteArrayOutputStream.toByteArray();

		return responseBytes;

	}

	/**
	 * 将字节数组转换成字符串
	 * 
	 * @param bytes
	 * @return
	 * @throws UnsupportedEncodingException
	 */

	private static String bytesToString(byte[] bytes) throws UnsupportedEncodingException {
		System.out.println(bytes);
		if (bytes != null) {
			String returnStr = new String(bytes, "utf-8");
			return returnStr;
		}
		return null;

	}
	
	public static void main(String args[]){
		
		try {
			POSTENTITY("http://192.168.50.62:9900/iwms/port/job/productStore/backup",null);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		
	}
}
